<template></template>
<script lang="ts" setup>
import * as u from '@/tool/function/util';
import { user, system } from '@/store';
import { decryptSM4 } from '@/common/utils/util';
import systemConfig from '@/common/config';
import commonApi from '@/api/modules/common';
import { onShow,onLaunch,onHide } from '@dcloudio/uni-app';
onShow(async (option:any) => {  
 // 页面加载时检测初始方向
 checkOrientation();
 
 // 监听屏幕方向变化
 window.addEventListener('orientationchange', checkOrientation);
})
onLaunch(()=>{
    
})
onHide(()=>{
    // 页面卸载时移除监听
    window.removeEventListener('orientationchange', checkOrientation);
})
onUnmounted(() => {
    window.removeEventListener('orientationchange', checkOrientation);
})
onBeforeMount(async () => {
	if(!user.defaultSM2PublicKey&&!user.defaultSM4Key){
        let secretStr=u.getCookie("USERSECRET");
        if(secretStr){
            let secret=JSON.parse(decryptSM4(secretStr,system.defaultSM4Key));
            await user.setUserSecret(secret);
        }else{
            u.delCookie(systemConfig.localTokenName);
            u.delCookie(systemConfig.localRefreshTokenName);
        }
    }
  	//置过app的秘钥
	if(!system.appCryptoFlag){
		await system.setAppCryptoFlag(true);
		await $api.common.setAppSecretKey.post();
	}

    if(systemConfig.zlbEnabled){
        ZWJSBridge.getUUID({}).then((res :any) => {
			system.setZlbFlag(true)
		}).catch((err:any) => {
			system.setZlbFlag(false)
            if(systemConfig.refreshTokenEnabled&&!system.zlbFlag){
                let refreshToken=user.refreshToken;
                if(!refreshToken){
                    refreshToken=u.getCookie(systemConfig.localRefreshTokenName);
                    user.setToken(u.getCookie(systemConfig.localTokenName),refreshToken)
                }
            }else{
                let accessToken=user.accesstoken;
                if(!accessToken){
                    accessToken=u.getCookie(systemConfig.localTokenName);
                    user.setToken(accessToken,u.getCookie(systemConfig.localRefreshTokenName))
                }
            }
            })
    }else{
        if(systemConfig.refreshTokenEnabled&&!system.zlbFlag){
            let refreshToken=user.refreshToken;
            if(!refreshToken){
                refreshToken=u.getCookie(systemConfig.localRefreshTokenName);
                user.setToken(u.getCookie(systemConfig.localTokenName),refreshToken)
            }
        }else{
            let accessToken=user.accesstoken;
            if(!accessToken){
                accessToken=u.getCookie(systemConfig.localTokenName);
                user.setToken(accessToken,u.getCookie(systemConfig.localRefreshTokenName))
            }
        }
    }
	getSessionInfo()
});
const getSessionInfo=async ()=>{
    let default_userInfo = u.getCookie('USER_INFO');
    if(!user.userId&&default_userInfo){
        user.setUser(JSON.parse(decryptSM4(default_userInfo,system.defaultSM4Key)));
    }else{
        if (user.accesstoken) {
            const sessionData = {};
            await commonApi.common.getUserInfo.post(sessionData);
        }
    }
}
const checkOrientation=()=> {
    const systemInfo=uni.getSystemInfoSync();
    const screenWidth =systemInfo.screenWidth;
    const screenHeight = systemInfo.screenHeight;
    let orientation = (screenWidth>screenHeight)?'landscape':'portrait'; 
    //#ifdef H5
    if(system.orientation&&system.orientation!==orientation){
        window.location.reload();
    }
    //#endif
    system.orientation=orientation;
    if(orientation === 'landscape'){
        /****
        uni.showModal({
			title:'系统提示',
			content:'横屏模式下，部分功能可能无法正常使用，请切换为竖屏模式后操作',
			showCancel:false
		})
         ***/
        
    }
    
}
</script>
<style>
/* #ifdef APP-PLUS-NVUE */
@import './theme/scss/nvue.css';

	body.pages-index-index uni-page-body,
	body {
		padding-bottom: 0 !important;
	}
	text{
		font-family:"阿里巴巴普惠体";
	}
	
/* #endif */

/* #ifndef APP-PLUS-NVUE */

@import './theme/scss/noNvue.css';

/* #endif */

@import './static/iconfont/basefont.css';
@import './static/iconfont/iconfont.css';
/* #ifdef H5 */

body::-webkit-scrollbar,
div::-webkit-scrollbar,
*::-webkit-scrollbar {
  display: none;
}

/* #endif */
</style>
